package com.gph.saviorframework.mq.api.impl.rocketmq;

import com.alibaba.rocketmq.remoting.protocol.RemotingCommand;
import com.gph.saviorframework.mq.api.impl.authority.AuthUtil;
import com.gph.saviorframework.mq.api.impl.authority.SessionCredentials;

import static com.gph.saviorframework.mq.api.impl.authority.SessionCredentials.*;

/**
 * Created by root on 16-7-10.
 */
public class ClientRPCHook extends AbstractRPCHook {

    private SessionCredentials sessionCredentials;

    public ClientRPCHook(SessionCredentials sessionCredentials) {
        this.sessionCredentials = sessionCredentials;
    }

    public void doBeforeRequest(String remoteAddr, RemotingCommand request){
        byte[] total = AuthUtil.combineRequestContent(request,
                parseRequestContent(request, sessionCredentials.getAccessKey()));
        String signature = AuthUtil.calSignature(total, sessionCredentials.getSecretKey());
        request.addExtField(Signature, signature);
        request.addExtField(AccessKey, sessionCredentials.getAccessKey());
    }

    public void doAfterResponse(String remoteAddr, RemotingCommand request, RemotingCommand response)  {

    }
}
